<?php


namespace app\wechat\controller;


use app\BaseController;
use think\cache\driver\Redis;
use think\facade\Db;

class Goods extends AuthBase
{
    /**
     * 计算价格
     */
    public function calculate(){
        $data = input('post.');
        if (!$data['buy_date']){
            return $this->show(0,'请先填写购买日期');
        }
        if (!$data['buy_price']){
            return $this->show(0,'请先填写购买价格');
        }
        if (!empty($data['target_price'])) {
            $target_price = $data['target_price'];
            //先算出应该使用时长
            $serve_day=ceil($data['buy_price']/$data['target_price']);
            //再给购买日期增加上使用时长就是预计到期日
            $newDateTimestamp = strtotime($data['buy_date']. " + $serve_day days");
            $use_date = date('Y-m-d', $newDateTimestamp);
        }
        if (!empty($data['serve_day'])) {
            $serve_day=$data['serve_day'];
            $target_price = round($data['buy_price']/$serve_day,2);
            //再给购买日期增加上使用时长就是预计到期日
            $newDateTimestamp = strtotime($data['buy_date']. " + $serve_day days");
            $use_date = date('Y-m-d', $newDateTimestamp);
        }
        if (!empty($data['use_date'])) {
            $use_date=$data['use_date'];
            $serve_day=get_serve_day($data['buy_date'],$use_date);
            $target_price = round($data['buy_price']/$serve_day,2);
        }
        $result['target_price']=$target_price;
        $result['serve_day']=$serve_day;
        $result['use_date']=$use_date;
        return $this->show(1,'',$result);
    }
    /**
     * 添加物品
     */
    public function add(){
        if ($this->isVip==1){
            //如果不是会员判断是否超上限
            $count=Db::name('goods')
                ->where('user_id',$this->userId)
                ->count();
            if ($count>=8){
                return $this->show(-2,'请购买会员');
            }
        }
        $data = input('post.');
        $data['buy_date']=strtotime($data['buy_date']);
        $data['use_date']=strtotime($data['use_date']);
        if ($data['buy_date']>time()){
            return show(0,'购买日期不能大于今天');
        }
        if ($data['use_date']<time()){
            return show(0,'使用日期不能小于今天');
        }
        if ($data['buy_price']<0){
            return show(0,'金额必须大于0');
        }
        $data['money']=$data['buy_price'];
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['user_id'] = $this->userId;
        Db::startTrans();
        try {
            //添加资产
            Db::name('goods')->insert($data);
            //修改个人用户表
            Db::name('user')->where('id',$this->userId)
                ->inc('asset_price',$data['buy_price'])
                ->update();
            Db::commit();
            return show(1, '添加成功');
        } catch (\Exception $e) {
            Db::rollback();
            return show(0, $e->getMessage());
        }
    }
    /**
     * 计算价格
     */
    public function calculate_sell(){
        $data = input('post.');
        if (!$data['buy_date']){
            return $this->show(0,'请先填写购买日期');
        }
        if (!$data['buy_price']){
            return $this->show(0,'请先填写购买价格');
        }
        if (!$data['sell_date']){
            return $this->show(0,'请先填写退役时间');
        }
        if (!$data['sell_price']){
            //算出使用时长
            $use_day=get_serve_day($data['buy_date'],$data['sell_date']);
            if ($use_day==0){
                $use_day=1;
            }
            //算出日均价格
            $target_price = round($data['buy_price']/$use_day,2);
        }
        if (!empty($data['sell_price'])){
            //算出使用时长
            $use_day=get_serve_day($data['buy_date'],$data['sell_date']);
            if ($use_day==0){
                $use_day=1;
            }
            //算出日均价格
            //算出价格
            $price=$data['buy_price']-$data['sell_price'];
            $target_price = round($price/$use_day,2);
        }
        $result['target_price']=$target_price;
        $result['serve_day']=$use_day;
        $result['level']=get_level($target_price);

        return $this->show(1,'',$result);
    }
    /**
     * 添加退役物品
     */
    public function add_sell(){
        if ($this->isVip==1){
            //如果不是会员判断是否超上限
            $count=Db::name('goods')
                ->where('user_id',$this->userId)
                ->count();
            if ($count>=8){
                return $this->show(-2,'请购买会员');
            }
        }
        $data = input('post.');
        $data['buy_date']=strtotime($data['buy_date']);
        $data['use_date']=strtotime($data['sell_date']);
        $data['sell_date']=strtotime($data['sell_date']);
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['money'] = $data['buy_price'];
        $data['user_id'] = $this->userId;
        Db::startTrans();
        try {
            //添加资产
            Db::name('goods')->insert($data);
            //修改个人用户表
            Db::name('user')->where('id',$this->userId)
                ->inc('sell_price',$data['sell_price'])
                ->update();
            Db::name('user')->where('id',$this->userId)
                ->inc('asset_price',$data['money'])
                ->update();
            Db::commit();
            return show(1, '添加成功');
        } catch (\Exception $e) {
            Db::rollback();
            return show(0, $e->getMessage());
        }
    }
    public function detail(){
        $id=input('get.id');
        $goods=Db::name('goods')
            ->where('id',$id)
            ->find();
        $model['name']=$goods['name'];
        $model['status']=$goods['status'];
        if ($model['status']==1){
            $yi_day=get_serve_time_day($goods['buy_date'],time());
            if ($yi_day<1){
                $yi_day=1;
            }
            $rijun=round($goods['money']/$yi_day,2);
            if (time()>$goods['use_date']) {
                //如果实际时间已经大于预计服役时间则算是超服役
                $model['status'] = 3;
                $juli=get_serve_time_day($goods['use_date'],time());
                $baifenbi=100;
            }else{
                $yuji=get_serve_time_day($goods['buy_date'],$goods['use_date']);
                $juli=get_serve_time_day(time(),$goods['use_date']);

                $baifenbi=round($yi_day/$yuji*100);
            }
        }else{
            //如果是退役的
            $yi_day=$goods['serve_day'];
            $rijun=$goods['target_price'];
            $juli=0;
            $baifenbi=100;
        }
        $model['yi_day']=$yi_day;
        $model['baifenbi']=$baifenbi;
        $model['rijun']=$rijun;
        $model['sell_price']=$goods['sell_price'];
        $model['level']=get_level($rijun);
        $model['buy_date']=date("Y-m-d",$goods['buy_date']);
        $model['use_date']=date("Y-m-d",$goods['use_date']);
        $model['juli_date']=$juli;
        $model['money']=$goods['money'];
        $model['buy_price']=$goods['buy_price'];
        $model['addition_price']=Db::name('goods_addition')
            ->where('goods_id',$goods['id'])
            ->sum('price');
        $model['net_price']=$goods['money']-$goods['sell_price'];
        $model['addition_list']=Db::name('goods_addition')
            ->where('goods_id',$goods['id'])
            ->order('create_time desc')
            ->column('id,price,remark');
        return $this->show(1,'',$model);
    }
    public function add_price(){
        $data=input('post.');
        $goods=Db::name('goods')
            ->where(['id'=>$data['id'],'user_id'=>$this->userId,'status'=>1])
            ->find();
        if (!$goods){
            return $this->show(0,'物品状态无法添加附加');
        }
        $data['goods_id']=$goods['id'];
        $data['user_id']=$this->userId;
        if ($data['price']<=0){
            return show(0,'金额必须大于0');
        }
        unset($data['id']);
        Db::startTrans();
        try {
            Db::name('goods_addition')->insert($data);
            Db::name('goods')
                ->where(['id'=>$goods['id'],'user_id'=>$this->userId])
                ->inc('money',$data['price'])
                ->update();
            Db::name('user')
                ->where(['id'=>$this->userId])
                ->inc('asset_price',$data['price'])
                ->update();
            Db::commit();
            return show(1, '添加成功');
        } catch (\Exception $e) {
            Db::rollback();
            return show(0, $e->getMessage());
        }
    }
    //删除附加价格
    public function del_price(){
        $id=input('get.id');
        $res=Db::name('goods_addition')
            ->where('id',$id)
            ->find();
        $goods=Db::name('goods')
            ->where('id',$res['goods_id'])
            ->where('user_id',$this->userId)
            ->where('status',1)
            ->find();
        if (!$goods){
            return $this->show(0,'此商品已无法删除');
        }
        Db::startTrans();
        try {
            //删除附加表
            Db::name('goods_addition')->delete($id);
            //增加总值
            Db::name('goods')
                ->where(['id'=>$goods['id'],'user_id'=>$this->userId])
                ->dec('money',$res['price'])
                ->update();
            //用户总价减少
            Db::name('user')
                ->where(['id'=>$this->userId])
                ->dec('asset_price',$res['price'])
                ->update();
            Db::commit();
            return show(1, '删除成功');
        } catch (\Exception $e) {
            Db::rollback();
            return show(0, $e->getMessage());
        }
    }
    public function update_status(){
        $data=input('post.');
        $data['use_date']=strtotime($data['use_date']);
        $data['sell_date']=$data['use_date'];
        $data['sell_price']=$data['sell_price'];
        //算一下日均
        $goods=Db::name('goods')
            ->where('id',$data['id'])
            ->where('user_id',$this->userId)
            ->find();
        if ($data['sell_date']<=$goods['buy_date']){
            return show(0,'退役日期不能小于购买日期');
        }
        $jingzhi=$goods['money']-$data['sell_price'];
        $rijun=round($jingzhi/get_serve_time_day($goods['buy_date'],$data['use_date']),2);
        $data['target_price']=$rijun;
        $data['serve_day']=get_serve_time_day($goods['buy_date'],$data['use_date']);
        $data['status']=2;
        Db::startTrans();
        try {
            unset($data['id']);
            Db::name('goods')
                ->where(['id'=>$goods['id'],'user_id'=>$this->userId])
                ->update($data);
            Db::name('user')
                ->where(['id'=>$this->userId])
                ->inc('sell_price',$data['sell_price'])
                ->update();
            Db::commit();
            return show(1, '退役成功');
        } catch (\Exception $e) {
            Db::rollback();
            return show(0, $e->getMessage());
        }
    }
    //
    public function formula(){
        $data=[
            0=>[
                'name'=>'艰苦朴素',
                'content'=>'小于3元',
            ],
            1=>[
                'name'=>'勤俭节约',
                'content'=>'3~7.2元',
            ],
            2=>[
                'name'=>'精打细算',
                'content'=>'7.2~10.8元',
            ],
            3=>[
                'name'=>'大手大脚',
                'content'=>'10.8~14.6元',
            ],
            4=>[
                'name'=>'穷奢极欲',
                'content'=>'大于14.6元',
            ]

        ];
        return $this->show(1,'',$data);
    }
    /**
     * 删除
     */
    public function del(){
        $id=input('get.id');
        $goods=Db::name('goods')
            ->where('id',$id)
            ->find();
        Db::startTrans();
        try {
            Db::name('goods')->delete($id);
            Db::name('user')
                ->where(['id'=>$this->userId])
                ->dec('asset_price',$goods['money'])
                ->update();
            if ($goods['sell_price']>0){
                Db::name('user')
                    ->where(['id'=>$this->userId])
                    ->dec('sell_price',$goods['sell_price'])
                    ->update();
            }
            Db::name('goods_addition')
                ->where('goods_id',$id)
                ->delete();
            Db::commit();
            return show(1, '删除成功');
        } catch (\Exception $e) {
            Db::rollback();
            return show(0, $e->getMessage());
        }
    }
}